home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 February: Tool Chest / Dev.CD Feb 00 TC.toast / pc / tool chest / development kits / hypercard related / xcmds & xfcns / byrne's xcmds&xfcns / source / geteof.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-06  |  4.5 KB  |  177 lines

  1. /*
  2.  
  3.     GetEOF XCMD v1.2
  4.     
  5.     ©1990-1 Apple Computer, Inc.; by Mike Byrne
  6.     
  7.     This XCMD takes a full pathname, converts it to a volume reference and a partial pathname, 
  8.     opens the file, reads the EOF number, closes the file, and returns the EOF.
  9.     
  10.     Form:
  11.     GetEOF(<full pathname>)
  12.     
  13.     # the MPW 3.2 build commands:
  14.     C -b GetEOF.c -mbg off
  15.         Link -w -t STAK -c WILD -rt XFCN=602 ∂
  16.             -m ENTRYPOINT ∂
  17.             -sg GetEOF ∂
  18.             GetEOF.c.o ∂
  19.             "{Libraries}HyperXLib.o" ∂
  20.             "{Libraries}Runtime.o" ∂
  21.             "{Libraries}Interface.o" ∂
  22.             "{CLibraries}StdCLib.o" ∂
  23.             -o "some stack"
  24. */
  25.  
  26. #include <Types.h>
  27. #include <string.h>
  28. #include <Memory.h>
  29. #include <Files.h>
  30. #include <Packages.h>
  31. #include "HyperXCmd.h"
  32.  
  33. #define NULL  0L
  34. #define NIL  0L
  35.  
  36. #define kNumParams 1
  37.  
  38. /* prototypes */
  39. void ErrorBack(XCmdPtr paramPtr, char *message);
  40. void MoveLockParams ( XCmdPtr paramPtr, short paramCount );
  41. void UnlockParams  ( XCmdPtr paramPtr, short paramCount );
  42.  
  43.  
  44. pascal void EntryPoint(XCmdPtr paramPtr)
  45. {
  46.     short         i,j, refNum;
  47.     long        theEOF = 0;
  48.     char        volName[34];
  49.     char        ppathName[301];
  50.     short        vRefNum;
  51.  
  52.  
  53.     /* move high and lock the parameters. */
  54.     MoveLockParams(paramPtr, paramPtr->paramCount);
  55.  
  56.     /* check for copyright or syntax help request */
  57.     if (!strcmp( (char*)*paramPtr->params[0], "!") ) {
  58.         ErrorBack(paramPtr, "v1.2, ©1990-1 Apple Computer, Inc.; by Mike Byrne");
  59.         UnlockParams(paramPtr, paramPtr->paramCount);
  60.         return;
  61.     } else if (!strcmp ( (char*)*paramPtr->params[0], "?") ) {
  62.         ErrorBack(paramPtr, "GetEOF syntax is 'GetEOF(<full pathname>)'");
  63.         UnlockParams(paramPtr, paramPtr->paramCount);
  64.         return;
  65.     }
  66.  
  67.     /* not a copyright or help request.       */     
  68.     /* check for correct number of parameters */
  69.     if (paramPtr->paramCount != kNumParams) {
  70.         ErrorBack(paramPtr, "Error: GetEOF syntax is 'GetEOF(<full pathname>)'");
  71.         UnlockParams(paramPtr, paramPtr->paramCount);
  72.         return;
  73.     }
  74.         
  75.     /*  move high and lock the parameters... */
  76.     MoveLockParams(paramPtr, kNumParams);
  77.  
  78.     /*  extract the volume name from the handle, copy to a pas string,
  79.          and get the volume reference number of the volume              */
  80.     for (i=0; ((*(paramPtr->params[0]))[i] != ':' && (i < 33)); i++) 
  81.         { volName[i] = (*(paramPtr->params[0]))[i]; }
  82.     volName[i] = ':';
  83.     volName[i+1] = '\0'; 
  84.     c2pstr(volName);
  85.     vRefNum = 0;
  86.     
  87.     if (SetVol(volName, vRefNum) != noErr) {                        // toolbox
  88.         ErrorBack(paramPtr, "Error: Could not set the default volume");
  89.         UnlockParams(paramPtr, kNumParams);
  90.         return;
  91.     }
  92.     
  93.     if (GetVol(&volName, &vRefNum) != noErr) {                        // toolbox
  94.         ErrorBack(paramPtr, "Error: Could not find the volume requested.");
  95.         UnlockParams(paramPtr, kNumParams);
  96.         return;
  97.     }
  98.  
  99.     /* now, copy the rest of the pathname to the partial pathname and convert it. */
  100.     for (j=i; (j <= strlen((*(paramPtr->params[0]))) && (j < 300)); j++) 
  101.         { ppathName[j-i] = (*(paramPtr->params[0]))[j]; }
  102.     c2pstr(ppathName);
  103.         
  104.     /* open the file  */
  105.     if ( FSOpen(ppathName, vRefNum, &refNum) != noErr) {            // toolbox
  106.         ErrorBack(paramPtr, "Error: Cannot open file.");
  107.         UnlockParams(paramPtr, kNumParams);
  108.         return; 
  109.     }
  110.     
  111.     /* now, get the eof (use j for the logEOF) */
  112.     if ( GetEOF(refNum, &theEOF) != noErr) {                        // toolbox
  113.         ErrorBack(paramPtr, "Error: Cannot find end-of-file.");
  114.         UnlockParams(paramPtr, kNumParams);
  115.         return;
  116.     }
  117.     
  118.     /*  now, close the file */
  119.     FSClose(refNum);                                                // toolbox
  120.  
  121.     /* now, convert the number to a string and send it back. */
  122.     NumToStr(paramPtr, theEOF, &ppathName);
  123.     p2cstr(ppathName);
  124.     ErrorBack(paramPtr, ppathName);
  125.     UnlockParams(paramPtr, kNumParams);
  126.     return;
  127. }
  128.  
  129.  
  130.  
  131.  
  132.  
  133.     
  134. /* allocate and load up paramPtr->returnValue with a string 
  135.    -------------------------------------------------------- */
  136. void ErrorBack(XCmdPtr paramPtr, char *message)
  137. {
  138.     Handle  mesHnd;
  139.  
  140.     /*
  141.         Allocate space for an error message.
  142.         Copy the string into it.
  143.         Return the handle to HyperCard.
  144.     */
  145.     mesHnd = NewHandle((long)(strlen(message)+1));                    /* toolbox */
  146.     if (mesHnd == nil) return;
  147.     strcpy((char *)*mesHnd,message);
  148.     paramPtr->returnValue = mesHnd;
  149. }
  150.  
  151.  
  152.  
  153. /*  move high and lock down all parameters (watch that these are read-only) 
  154.     ----------------------------------------------------------------------- */
  155. void MoveLockParams ( XCmdPtr paramPtr, short paramCount )
  156. {
  157.     short i;
  158.     
  159.     for(i=0; i <= paramCount-1; i++)
  160.     {
  161.         MoveHHi(paramPtr->params[i]);    /* toolbox */
  162.         HLock(paramPtr->params[i]);        /* toolbox */
  163.     }
  164. }
  165.  
  166.  
  167.  
  168.  
  169. /* unlock all parameter handles in the XCmdBlock  
  170.    ---------------------------------------------  */
  171. void UnlockParams  ( XCmdPtr paramPtr, short paramCount )
  172. {    short i;
  173.     
  174.     for(i=0; i <= paramCount-1; i++)
  175.         { HUnlock(paramPtr->params[i]);}    /* toolbox */
  176. }
  177.